import {
	getOpenId,
	getUserPhone,
	login,
	register,
} from '@/api/login'
import {
	authentication,
} from '@/api/pagesMine'
export default {
	namespaced: true,
	state: {
		// 微信基础信息
		WX_BASIC_INFO: {},
		// 用户信息
		USER_INFO: uni.getStorageSync('USER_INFO') || null,
		// 定位信息
		LOCATION_INFO: {},
		//微信用户openId
		USER_OPENID: uni.getStorageSync('OPENID') || '',
		//当前认证企业信息
		COMPANY_INFO: uni.getStorageSync('COMPANY_INFO') || null,

	},
	mutations: {
		// 更新定位信息
		COMMIT_LOACTION_INFO(state, data) {
			uni.setStorage({
				key: 'LOCATION_INFO',
				data
			})
			state.LOCATION_INFO = data
		},
		// 更新用户信息
		COMMIT_USER_INFO(state, data) {
			uni.setStorage({
				key: 'USER_INFO',
				data
			})
			state.USER_INFO = data
		},
		// 更新当前认证企业信息
		CURRENT_COMPANY_INFO(state, data) {
			uni.setStorage({
				key: 'COMPANY_INFO',
				data
			})
			state.COMPANY_INFO = data
		},
		// 更新用户信息openId
		COMMIT_USER_OPENID(state, data) {
			uni.setStorage({
				key: 'OPENID',
				data
			})
			state.USER_OPENID = data
		},
	},
	actions: {
		//获取openId
		getWxOpenId({
			commit
		}) {
			return new Promise((resolve, reject) => {
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						getOpenId({
							code: loginRes.code
						}).then(res => {
							commit('COMMIT_USER_OPENID', JSON.parse(res.data).openid || '')
							// commit('COMMIT_USER_OPENID', 'oIalr1x0YR0ESMl-XrOHf2YlSabs')
							resolve(res)
						}).catch(error => {
							reject(error)
						});
					}
				});
			});
		},
		//获取手机号
		getWxPhone(code) {
			return new Promise((resolve, reject) => {
				getUserPhone({
					code: code
				}).then(res => {
					resolve(res)
				}).catch(error => {
					reject(error)
				});
			})
		},
		/* 登录&注册 */
		Register({
			commit
		}, userInfo) {
			const username = userInfo.userName.trim()
			const phone = userInfo.mobile.trim()
			const idCard = userInfo.idNumber.trim()
			const openId = userInfo.userId.trim()
			const code = userInfo.code.trim()
			return new Promise((resolve, reject) => {
				login(username, phone, idCard, openId, code).then(res => {
					let currentCompany = null
					if (res.data.userCompanyRecords && res.data.userCompanyRecords.length > 0) {
						currentCompany = res.data.userCompanyRecords.find(item => item.def == 0)
					}
					commit('CURRENT_COMPANY_INFO', currentCompany)
					commit('COMMIT_USER_INFO', res.data)
					resolve(res)
				}).catch(error => {
					reject(error)
				})
			})
		},
		/* 登录 */
		Login({
			commit
		}, userInfo) {
			// const mobile = userInfo.mobile.trim()
			const openId = userInfo.userId.trim()
			// const code = userInfo.code.trim()
			return new Promise((resolve, reject) => {
				register({
					// mobile: mobile,
					// code: code,
					userId: openId
				}).then(res => {
					let currentCompany = null
					resolve(res)
					if (res.code == 200) {
						if (res.data.userCompanyRecords && res.data.userCompanyRecords.length > 0) {
							currentCompany = res.data.userCompanyRecords.find(item => item.def == 0)
						}
						commit('CURRENT_COMPANY_INFO', currentCompany)
						commit('COMMIT_USER_INFO', res.data)
					}
				}).catch(error => {
					reject(error)
				})
			})
		},
		/* 认证 */
		Authentication({
			commit
		}, userInfo) {
			const openId = userInfo.userId.trim()
			return new Promise((resolve, reject) => {
				authentication(openId).then(res => {
					let currentCompany = null
					resolve(res)
					if (res.code == 200) {
						if (res.data.userCompanyRecords && res.data.userCompanyRecords.length > 0) {
							currentCompany = res.data.userCompanyRecords.find(item => item.def == 0)
						}
						commit('CURRENT_COMPANY_INFO', currentCompany)
						commit('COMMIT_USER_INFO', res.data)
					}
				}).catch(error => {
					reject(error)
				})
			})
		},
		// 退出登录
		logout({
			commit
		}) {
			/* 不能清除openid登录要用 */
			commit('COMMIT_USER_INFO', null)
			uni.removeStorageSync('USER_INFO')
			uni.removeStorageSync('COMPANY_INFO')

		},
	}
}